package com.changgou.goods.controller;

import com.changgou.goods.pojo.Goods;
import com.changgou.goods.service.SpuService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author seafwg
 * @Date 2021/12/10 5:32 下午
 * @Version 1.0
 * @description xxx
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;


    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping(value = "/del/{id}")
    public Result logicDelete(@PathVariable Long id) {
        int i = spuService.logicDelete(id);
        return new Result(true, StatusCode.OK, "删除成功", i);
    }

    /**
     * 批量上架商品
     * @param ids
     * @return
     */
    @PutMapping(value = "/put/many")
    public Result putMany(@RequestBody Long[] ids) {
        int i = spuService.putMany(ids);
        return new Result(true, StatusCode.OK, "商品批量上架成功", i);
    }

    /**
     * 商品的上架
     * @param id
     * @return
     */
    @PutMapping(value = "/put/{id}")
    public Result put(@PathVariable Long id) {
        spuService.put(id);
        return new Result(true, StatusCode.OK, "商品上架成功");
    }

    /**
     * 商品下架
     * @param id
     * @return
     */
    @PutMapping(value = "/pull/{id}")
    public Result pull(@PathVariable Long id) {
        spuService.pull(id);
        return new Result(true, StatusCode.OK, "商品下架成功");
    }

    /**
     * 商品审核
     * @param id
     * @return
     */
    @PutMapping(value = "/audit/{id}")
    public Result audit(@PathVariable(name = "id") Long id) {
        spuService.audit(id);
        return new Result(true, StatusCode.OK, "商品审核成功");
    }

    /**
     * 新增商品
     * @param goods
     * @return
     */
    @PostMapping(value = "/add")
    public Result add(@RequestBody Goods goods) {
        spuService.saveGoods(goods);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 根据spuId查询商品
     * @param spuId
     * @return
     */
    @GetMapping(value = "/get/{spuId}")
    public Result<Goods> findGoodsById(@PathVariable(name = "spuId") Long spuId) {
        Goods goods = spuService.findById(spuId);
        return new Result<>(true, StatusCode.OK, "查询成功", goods);
    }
}
